package list.functions.accumulator;

/**
 *
 * An accumulator that returns the maximum value of all the values given
 * to the accumulator before calling getAccumulatedValue.
 * 
 * Fully Tested: No
 * Fully Documented: No
 * Created: 21-Apr-2012
 * Last Updated: 21-Apr-2012
 * 
 * @param <T> 
 * @author Shimu Wu
 */
public class MinAccumulator<T extends Comparable<T>>
        implements Accumulator<T, T> {

    private T minSoFar;

    private final T MAX_VALUE;
    
    /**
     * @param maxValue the largest value in set T, for instance, for numbers
     * it will be positive infinity
     */
    public MinAccumulator(T maxValue) {
        this.MAX_VALUE = maxValue;
        this.reset();
    }

    @Override
    public final void reset() {
        this.minSoFar = this.MAX_VALUE;
    }
    
    @Override
    public void accumulate(T element) {
        // Only assigns a new max if the new element is strictly
        // less than minSoFar
        if (minSoFar.compareTo(element) > 0) {
            minSoFar = element;
        }
    }

    @Override
    public T getAccumulatedValue() {
        return minSoFar;
    }
}
